import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/get_feign_emergency_team_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/get_feign_team_member_list_page_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/common/risk_tree_entity.dart';

///应急队伍清单
class EmergencyTeamListController extends GetxController {
  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 选中的节点
  RiskTreeEntity selectedEntity = RiskTreeEntity();

  /// 擅长专业
  String beGoodAtMajor = '';
  String searchBeGoodAtMajor = '';

  /// 技术职称
  String technology = '';
  String searchTechnology = '';

  /// 队员姓名
  String searchSelect = '';
  RxString searchX = ''.obs;

  Future<List<GetFeignTeamMemberListPageEntity>> feignTeamMemberListPage(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<GetFeignTeamMemberListPageEntity>>(
      Interface.getFeignTeamMemberListPage,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'teamMemberName': searchX.value, // 队员姓名
        'beGoodAtMajor': beGoodAtMajor, //	擅长专业
        'technology': technology, // 技术职称
        'teamIds': teamIds, // 队伍ID集合
      },
    ).execute();
    List<GetFeignTeamMemberListPageEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  /// 筛选按钮
  void onFilterButtonPressed() {
    searchSelect = searchX.value; //向另一个地方插入值 那个地方也和这一样
    riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 重置
  void reset() {
    teamIds.clear();
    //获取物资点 清空
    for (var value in getFeignEmergencyTeamListEntityList) {
      if (value.checkboxSelected) {
        value.checkboxSelected = false;
      }
    }
    searchX.value = '';
    searchSelect = '';

    // 擅长专业
    beGoodAtMajor = '';
    searchBeGoodAtMajor = '';

    // 技术职称
    technology = '';
    searchTechnology = '';
    riskRightScaffoldKey.currentState?.closeEndDrawer();
    selectedEntity = RiskTreeEntity();
    globalKey.currentState!.onRefresh();
    update();
  }

  ///   'teamIds': teamIds, // 队伍ID集合
  List<String> teamIds = [];

  /// 确定
  void confirm() {
    teamIds.clear();
    //获取物资点
    for (var value in getFeignEmergencyTeamListEntityList) {
      if (value.checkboxSelected) {
        //加入
        // teamIds += '${value.id};';
        teamIds.add(value.id ?? '');
      }
    }

    Get.back();
    searchX.value = searchSelect;
    beGoodAtMajor = searchBeGoodAtMajor;
    technology = searchTechnology;
    globalKey.currentState!.onRefresh();
    update();
  }

  @override
  void onInit() {
    super.onInit();
    getFeignEmergencyTeamListEntity();
  }

  List<GetFeignEmergencyTeamListEntity> getFeignEmergencyTeamListEntityList = [];

  /// 应急队伍列表
  getFeignEmergencyTeamListEntity() async {
    final res = await HttpUtil()
        .generateGetRequest<List<GetFeignEmergencyTeamListEntity>>(
          Interface.getFeignEmergencyTeamList,
        )
        .execute();
    res.onSuccess((result) {
      if (result.data != null) {
        getFeignEmergencyTeamListEntityList = result.data ?? [];
      }
    });
    update();
  }

  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
